package redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class RedisDemoGeneric {
    // get 和 set
    public static void test01(Jedis jedis) {
        System.out.println("get 和 set");
        // 清除之前数据
        jedis.flushAll();
        String res = jedis.set("key", "111");
        System.out.println(res);
        jedis.set("key2", "222");
        jedis.set("key3", "333");
        jedis.set("key4", "444");
        jedis.set("key5", "555");
        System.out.println(jedis.get("key2"));
        System.out.println(jedis.get("key1"));
    }
    // keys 和 exists
    public static void test02(Jedis jedis) {
        System.out.println("keys 和 exists");
        Set<String> keys = jedis.keys("*");
        Set<String> keys1 = jedis.keys("*2");
        System.out.println("keys: " + keys);
        System.out.println("keys1: " + keys1);

        // exists
        boolean key1 = jedis.exists("key1");
        System.out.println("key1: " + key1);
        System.out.println(jedis.exists("key3"));
    }


    // del 和 expire ttl
    public static void test03(Jedis jedis) {
        System.out.println("del expire 和 ttl");
        long del = jedis.del("key", "key1", " key2");
        System.out.println("del: " + del);
        // expire
        long key4 = jedis.expire("key10", 200);
        System.out.println("key4: " + key4);
        System.out.println(jedis.ttl("key10"));
//        System.out.println(jedis.ttl("key4"));
        System.out.println(jedis.ttl("key4"));
    }

    // type
    public static void test04(Jedis jedis) {
        System.out.println("type");
        String key1 = jedis.type("key");
        System.out.println("key1: " + key1);
        jedis.lpush("key1", "1", "2", "3", "hello");
        System.out.println(jedis.type("key1"));
        jedis.set("h1", "hhhh");
        System.out.println(jedis.type("h1"));
    }

    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool(Constant.IP);
        try (Jedis jedis = jedisPool.getResource()){
//            test01(jedis);
//            test02(jedis);
//            test03(jedis);
            test04(jedis);
        }
    }
}
